package com.briup.cms.config;

import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.AuthorizationScope;
import springfox.documentation.service.SecurityReference;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * swagger配置类
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {

	@Autowired
	private ConfigProperties configProperties;

	@Bean
	public Docket createRestApi() {
		return new Docket(DocumentationType.SWAGGER_2)
				.apiInfo(apiInfo())
				.select()
				.apis(RequestHandlerSelectors.basePackage(configProperties.getSwaggerScanControllerPackage()))
				.paths(PathSelectors.any())
				.build()
				.securitySchemes(security())
				.securityContexts(securityContexts());
//				.ignoredParameterTypes();
	}

	private List<ApiKey> security() {
		return Collections.singletonList(
				new ApiKey("Authorization", "Authorization", "header")
		);
	}

	private List<SecurityContext> securityContexts() {
		// 例如，/auth/**
//		antPaths.addAll(ConfigConstants.SWAGGER_AUTH_PATH);
		List<String> antPaths = new ArrayList<>(Arrays.asList(configProperties.getAuthPath()));

		return Collections.singletonList(
				SecurityContext.builder()
						.securityReferences(defaultAuth())
						.forPaths(antPathsCondition(antPaths))
						.build()
		);
	}

	private List<SecurityReference> defaultAuth() {
		AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
		AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
		authorizationScopes[0] = authorizationScope;
		return Collections.singletonList(
				new SecurityReference("Authorization", authorizationScopes));
	}

	private Predicate<String> antPathsCondition(List<String> antPaths){

//		if(antPaths==null||antPaths.size()==0) {
//			antPaths = new ArrayList<>();
//			antPaths.add("/**");
//		}

		List<Predicate<String>> list = new ArrayList<>();

		antPaths.forEach(path->list.add(PathSelectors.ant(path)));

		return Predicates.or(list);

	}
	
	private ApiInfo apiInfo() {
		return new ApiInfoBuilder()
				.title(configProperties.getSwaggerTitle())
				.description(configProperties.getSwaggerDescription())
				.termsOfServiceUrl(configProperties.getSwaggerUrl())
				.version(configProperties.getSwaggerVersion())
				.build();
	}
}

